from zope.interface import implements, Interface

from Products.Five import BrowserView
from Products.CMFCore.utils import getToolByName
from plone.memoize.instance import memoize

from my315ok.researchevent import researcheventMessageFactory as _


class IeventView(Interface):
    """
    event view interface
    """

    def test():
        """ test method"""


class eventView(BrowserView):
    """
    event browser view
    """
    implements(IeventView)

    def __init__(self, context, request):
        self.context = context
        self.request = request

    @property
    def portal_catalog(self):
        return getToolByName(self.context, 'portal_catalog')

    @property
    def portal(self):
        return getToolByName(self.context, 'portal_url').getPortalObject()
    
    @memoize
    def getOccurrencetime(self):
        """
        fetch local time
        """
#        import pdb
#        pdb.set_trace()
        fmt = '%Y/%m/%d %H:%M:%S'
        import time
        from datetime import datetime
        from zope.i18n.locales import locales
        from zope.i18n.interfaces import IUserPreferredLanguages
        languages = IUserPreferredLanguages(self.request)
        langs = languages.getPreferredLanguages()
        if langs:
            parts = (langs[0].split('-') + [None, None])[:3]
        else:
            parts = parts = (None, None, None)
#        try:
#            local = self.request.environ['I18N_LANGUAGE']
#        except:
#            local ='zh'
            
        locales = locales.getLocale(*parts)
#        locales = locales.getLocale(local)
        formatter = locales.dates.getFormatter('dateTime', length='full') 
        from time import mktime
               
        ltime = self.context.occurrencetime
        di = time.strptime(ltime.strftime(fmt),fmt)
        dt = datetime.fromtimestamp(mktime(di))
        dt = formatter.format(dt)       
        return dt

    def test(self):
        """
        test method
        """
        dummy = _(u'a dummy string')

        return {'dummy': dummy}
